import Vue from 'vue'
import VueRouter from 'vue-router'

// 切换显示进图条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { name: 'login', path: '/login', component: () => import('@/views/Login.vue') },
  {
    name: 'home',
    path: '/home',
    component: () => import('@/views/Home.vue'),
    redirect: '/home/welcome',
    children: [
      { name: 'welcome', path: 'welcome', component: () => import('@/views/Welcome.vue') },
      { name: 'users', path: 'users', component: () => import('@/views/user/Users.vue') },
      { name: 'rights', path: 'rights', component: () => import('@/views/Power/Rights.vue') },
      { name: 'roles', path: 'roles', component: () => import('@/views/Power/Roles.vue') },
      { name: 'cate', path: 'categories', component: () => import('@/views/goods/Cate.vue') },
      { name: 'params', path: 'params', component: () => import('@/views/goods/Params.vue') },
      { name: 'goodslist', path: 'goods', component: () => import('@/views/goods/GoodsList.vue') },
      { name: 'addgoods', path: 'addgoods', component: () => import('@/views/goods/AddGoods.vue') },
      { name: 'orders', path: 'orders', component: () => import('@/views/Order/Order.vue') },
      { name: 'reports', path: 'reports', component: () => import('@/views/Reports/Reports.vue') }

    ]
  }
]

const router = new VueRouter({
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    if (sessionStorage.getItem('tokenUser')) {
      // 显示进度条
      NProgress.start()
      next()
    } else {
      next('/login')
    }
  }
})
// 结束进度条
router.afterEach((to, from) => {
  NProgress.done()
})

export default router
